package com.edu.ad.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.ad.service.PromotionAdService;
import com.edu.api.entity.ad.PromotionAd;
import com.edu.api.entity.ad.PromotionSpace;
import com.edu.api.entity.common.Code;
import com.edu.api.entity.common.Pageable;
import com.edu.api.entity.common.ResponseDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @description: 描述
 * @author: xnylh
 * @createDate: 2023/1/4 0004 16:41
 */
@RestController
@RequestMapping("ad")
public class PromotionAdController {
    @Autowired
    PromotionAdService promotionAdService;

    @GetMapping("/getPromotionAdList")
    public List<PromotionAd> getPromotionAdList(){
        QueryWrapper<PromotionAd> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status",0);
        return promotionAdService.list(queryWrapper);
    }

    @GetMapping("/getAdsBySpaceId/{spaceId}")
    public ResponseDTO getAdsBySpaceId(@PathVariable int spaceId){
        List<PromotionAd> promotionAds = promotionAdService.getAdsBySpaceId(spaceId);
        return new ResponseDTO<>(Code.GET_OK,"成功",promotionAds);
    }

    @PostMapping("/updateAdStatus")
    public ResponseDTO updateAdStatus(@RequestBody Map<String,String> map){
        return promotionAdService.updateAdStatus(Integer.valueOf(map.get("id")),map.get("spaceId"),map.get("status"),map.get("startTime"),map.get("endTime"));
    }

    @PostMapping("/addOrUpdateBackStageAd")
    public ResponseDTO addOrUpdateBackStageAd(@RequestBody PromotionAd ad, HttpServletRequest request){
        return promotionAdService.addOrUpdateBackStageAd(ad,request);
    }

    @GetMapping("/getAllAd")
    public ResponseDTO getAllSpace(Integer page, Integer size, PromotionAd ad) throws ParseException {
        Pageable pageable = new Pageable();
        pageable.setPage(page);
        pageable.setSize(size);
        return promotionAdService.getAllAd(pageable,ad);
    }


    @DeleteMapping("/deleteBackstageAd")
    public ResponseDTO deleteAd(@RequestBody List<Integer> ids){
        return promotionAdService.deleteAdByIds(ids);
    }
}
